<?php

namespace App\Http\Controllers\View;

use App\Http\Controllers\Controller;
use App\Models\TempEmail;
use App\Models\User;
use App\Tool\SendEmail;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;

class UsersController extends Controller
{


    public function __construct()
    {
        $this->middleware('auth')->only(['edit','destroy']);
        $this->middleware('guest')->only('create');
    }


    public function accountActive(Request $request)
    {
        $user_id = $request->input('user_id');
        $code = $request->input('code');

        $tempEmail = TempEmail::query()->where('user_id',$user_id)->first();

        if ($tempEmail->code != $code) {
            session()->flash('warning','激活码不正确');
            return redirect()->route('login');
        }


        $user = User::query()->where('id', $user_id)->first();
        $user->is_active = true;
        $user->save();

        session()->flash('success','激活成功');
        return redirect()->route('login');
    }



    //创建用户页
    public function create()
    {
        return view('users.create');
    }

    //显示用户
    public function show(User $user)
    {
        $posts = $user->getPosts();
        return view('users.show',compact('user','posts'));
    }

    //修改用户页
    public function edit(User $user)
    {
        if (!Gate::allows('isAuth',$user)) {
            abort(403);
        }
        return view('users.edit',compact('user'));
    }

    //会员列表页
    public function index()
    {
        
        $users = User::query()->paginate(10);
        return view('users.index',compact('users'));
    }



    //创建用户
    public function store(Request $request)
    {
        $this->validate($request,[
            "name" => ['required',Rule::unique("users"),'max:100'],
            "email" => ['required',Rule::unique("users")],
            "password" => ['required','min:6','confirmed'],
        ]);

        $user = User::query()->create([
            "name" => $request->name,
            "email" => $request->email,
            "password" => bcrypt($request->password)
        ]);

        $sendEmail = new SendEmail();
        $sendEmail->setTo($user->email);


        $tempEmail = TempEmail::query()->where('user_id', $user->id)->first();

        $user->code = $tempEmail->code;

        $sendEmail->sendEmailActive('emails.active',compact('user'));

        session()->flash('success','恭喜你,账户注册成功,请尽快完成激活');
        return redirect()->route('login');
    }

    //修改用户
    public function update(Request $request, User $user)
    {

        $this->validate($request,[
           'name' => ['required',Rule::unique('users')->ignore($user->id)],
           'password' => ['nullable','min:6','confirmed']
        ]);

        $data['name'] = $request->name;

        if ($request->password) {
            $data['password'] = bcrypt($request->password);
        }

        $user->update($data);

        session()->flash('success','修改成功');
        return redirect()->route('users.show',$user);
    }

    //删除用户
    public function destroy(User $user)
    {

        $this->authorize('destroy',$user);
        $user->delete();
        session()->flash('success','删除成功');
        return redirect()->back();
    }

    //查看粉丝
    public function getFans(User $user)
    {
        $fans = $user->fans()->paginate(8);
        return view('users.get_fans',compact('fans'));
    }

    //查看关注
    public function getFanings(User $user)
    {
        $fanings = $user->fanings()->paginate(8);
        return view('users.get_fanings',compact('fanings'));
    }

}
